//
//  RDConfigManager.h
//  RDelivery
//
//  Created by selmerzhang on 2021/8/9.
//  Copyright © 2021 Tencent. All rights reserved.
//

#ifndef RDConfigManager_h
#define RDConfigManager_h

#import "RDConfigServerJsonModel.h"
#import "RDeliverySDKSettings.h"
#import "RDeliveryPullConfigTask.h"
#import "RDeliverySystemBizDataDelegate.h"

@class RDConfigInfo;
@protocol RDConfigInfoListener, RDConfigEventListener;

/// 拉取配置的回调
/// @param configInfoArray 如果是按场景拉取，或按配置key拉取，则将拉到的配置通过configInfoArray返回
/// @param error 拉取错误信息
typedef void (^RDUpdateConfigCompletedHandler)(NSArray<RDConfigInfo *> * _Nullable configInfoArray, NSError * _Nullable error);

NS_ASSUME_NONNULL_BEGIN

@interface RDConfigManager : NSObject

/// 使用配置设置初始化
/// @param settings 配置设置
- (instancetype)initWithSettings:(RDeliverySDKSettings *)settings;

#pragma mark - Data
/// 获取配置信息
/// @param key 配置信息的key
- (nullable RDConfigInfo *)configInfoWithKey:(NSString *)key;

/// 从磁盘同步读取配置信息
/// @param key 配置信息的key
- (nullable RDConfigInfo *)readConfigInfoWithKeyFromDisk:(NSString *)key;

/// 是否已完成从磁盘中初始化数据
- (BOOL)hasDataInitialized;

/// 获取所有配置的key
- (NSArray<NSString *> *)allConfigKeys;

/// 获取所有配置
- (NSDictionary<NSString *,RDConfigInfo *> *)allConfigInfos;

///  清理磁盘+ 内存数据
- (void)clearAllCache;

/// 删除内存、磁盘中单个配置
/// @param key 需要删除配置的key
- (void)deleteDataWithKey:(NSString *)key;

/// 最近一次请求体
- (NSString *)lastPullJsonBody;

/// 配置拉取时间（单位是毫秒）
/// @param key 配置 key
- (NSInteger)getLastRequestServerTimeForKey:(NSString *)key;

/// 全量配置拉取时间（单位是毫秒）
- (NSInteger)getLastRequestServerTime;

#pragma mark - Update Config
/// 拉取配置并更新
/// @param pullType 拉取类型
/// @param keys 配置的key
/// @param sceneIds 场景id列表
/// @param source 请求来源
/// @param handler 拉到配置后的回调
- (void)updateConfigWithPullType:(RDConfigServerPullType)pullType
                            keys:(NSArray<NSString *> * _Nullable)keys
                        sceneIds:(NSArray<NSNumber *> * _Nullable)sceneIds
                   requestSource:(RDGetConfigRequestSource)source
                 completeHandler:(RDUpdateConfigCompletedHandler _Nullable)handler;

/// 拉取配置并更新
/// @param pullType 拉取类型
/// @param batchRequestStartTime 合并请求时间戳
/// @param keys 配置的key
/// @param sceneIds 场景id列表
/// @param source 请求来源
/// @param handler 拉到配置后的回调
- (void)updateConfigWithPullType:(RDConfigServerPullType)pullType
           batchRequestStartTime:(NSTimeInterval)batchRequestStartTime
                            keys:(NSArray<NSString *> * _Nullable)keys
                        sceneIds:(NSArray<NSNumber *> * _Nullable)sceneIds
                   requestSource:(RDGetConfigRequestSource)source
                 completeHandler:(RDUpdateConfigCompletedHandler _Nullable)handler;

/// 根据taskIds直接读取后台配置，配置不更新到本地
/// @param taskIds 配置任务Id
/// @param handler 数据回调
- (void)fetchConfigWithTaskIds:(NSArray<NSNumber *> *)taskIds
               completeHandler:(void(^ _Nullable)(NSArray<RDConfigInfo *> *configInfoArray, NSError * _Nullable error))handler;


/// 获取SDK配置
/// @param completion 回调
- (void)fetchSDKConfigWithCompletion:(void(^ _Nullable)(RDSDKConfigData *configData,
                                                        NSError * _Nullable error))completion;

#pragma mark - Add Experience List

- (void)addExpListWithQRCodeID:(NSString *)qrcodeID
                        idType:(NSString *)idType
                       idValue:(NSString *)idValue
                    completion:(void(^ _Nullable)(BOOL success, NSError * _Nullable error))completion;

#pragma mark - Setting
/// 设置定时拉取时间
/// @param duration 定时拉取时间间隔
- (void)setUpdateDuration:(NSTimeInterval)duration;

#pragma mark - Listener
/// 为配置值变更添加一个监听者
/// @param listener 监听者
/// @param key 配置数据的key
- (void)addConfigInfoListener:(id<RDConfigInfoListener>)listener forKey:(NSString *)key;

/// 移除配置值变更监听者
/// @param listener 监听者
/// @param key 配置数据的key
- (void)removeConfigInfoListener:(id<RDConfigInfoListener>)listener forKey:(NSString *)key;

/// 为配置事件添加一个监听者
/// @param listener 监听者
- (void)addConfigEventListener:(id<RDConfigEventListener>)listener;

/// 移除配置事件监听者
/// @param listener 监听者
- (void)removeConfigEventListener:(id<RDConfigEventListener>)listener;


#pragma mark - Reset
/// 切换用户id
/// @param guid 用户id
- (void)resetGuid:(NSString *)guid;

/// 切换环境
/// @param envId 环境ID
- (void)resetEnvironment:(NSString *)envId;

#pragma mark - SystemBizData
/// 设置其他封装RDeliverySDK的系统的业务数据delegate，如TAB
- (void)setSystemBizDataDelegate:(id<RDeliverySystemBizDataDelegate>)delegate;

#pragma mark - 监控联动
/// 当前实例的标签
- (NSString *)getHitSubTaskTags;

@end

NS_ASSUME_NONNULL_END

#endif /* RDConfigManager_h */
